package br.com.srv.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import br.com.srv.exception.FFServiceException;

public class PontoDAOImpl  extends SimpleJdbcDaoSupport implements PontoDAO {

	public PontoDAOImpl() {}
	
	public List<Map<String, String>> buscarPontoLikeCoordenada(String longitude, String latitude)
		throws FFServiceException {
		
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
	
		String sql = "SELECT PTP_ID, PTP_LNGC, PTP_LATC FROM ponto WHERE PTP_LNGC LIKE ? AND PTP_LATC LIKE ?";

		String parametros[] = {longitude+"%", latitude+"%"};
		
		List list = getJdbcTemplate().query(sql, parametros, new PontosMapper());
		
		try {
//			getConnection().close();
		} catch (CannotGetJdbcConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}

    private static class PontosMapper implements RowMapper {

		public Map<String, String> mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			Map<String, String> ponto = new HashMap<String, String>();
			ponto.put("ID", resultSet.getString(1));
			ponto.put("LNG", resultSet.getString(2));
			ponto.put("LAT", resultSet.getString(3));
			return ponto;
		}

    }
	
}
